package com.webtest.mvctest3;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2018/9/6 22:13
 */
@WebFilter(filterName = "filterDispatcher", urlPatterns = "*.action")
public class FilterDispatcher extends AbstractHttpFilter {
    private static final Logger LOGGER = LoggerFactory.getLogger(FilterDispatcher.class);

    @Override
    protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
        //1.获取servletPath
        String servletPath = request.getServletPath();
        String sPath = servletPath.substring(servletPath.lastIndexOf("/"));
        LOGGER.info(servletPath);
        String path = null;

        //2.判断servletPath，若其等于/product-input.action，则转发到/WEB-INF/app1/input.jsp
        if ("/product-input.action".equals(sPath)) {
            path = "/WEB-INF/app1/input.jsp";
        }

        //3.若其等于/product-save.action，则

        if (path != null) {
            request.getRequestDispatcher(path).forward(request, response);
            return;
        }

        chain.doFilter(request, response);
    }

    @Override
    protected void init() {

    }
}
